package com.wiener.chamilo.engine.utils;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import com.wiener.chamilo.engine.beans.Alumno;
import com.wiener.chamilo.engine.xml.beans.User;
import com.wiener.chamilo.engine.xml.beans.Users;

public class Utils {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File inputFile = new File("C://Chamilo//plantillas//ALUMNO.txt");
		String[] objectoAlumno = null;
		Alumno alumno = null;
		Users users = new Users();
		List<User> usuarios = new ArrayList<User>();
		User user = null;
		try {
			List<String> contents = FileUtils.readLines(inputFile);
			for (String line : contents) {
				//System.out.println(line);
				alumno = new Alumno();
				objectoAlumno = line.split(";");
				//System.out.println(objectoAlumno.length);
				if (objectoAlumno.length > 0 && objectoAlumno.length <= 12 && objectoAlumno.length != 1) {
					alumno.setFirstname(objectoAlumno[0]);
					alumno.setLastname(objectoAlumno[1]);
					//alumno.setStatus(objectoAlumno[2]);
					alumno.setEmail(objectoAlumno[3]);
					alumno.setLoginname(objectoAlumno[4]);
					alumno.setPassword(objectoAlumno[5]);
					alumno.setOriginal_user_id_name(objectoAlumno[6]);
					alumno.setOriginal_user_id_value(objectoAlumno[7]);
					alumno.setLanguage(objectoAlumno[8]);
					alumno.setPhone(objectoAlumno[9]);
					alumno.setExpiration_date(objectoAlumno[10]);
					alumno.setUrl(objectoAlumno[11]);
					
					/*user= new User(alumno.getFirstname(),alumno.getLastname(),alumno.getStatus(),alumno.getEmail(),alumno.getLoginname(),alumno.getPassword(),
							alumno.getOriginal_user_id_name(),alumno.getOriginal_user_id_value(),alumno.getLanguage(),alumno.getPhone(),alumno.getExpiration_date(),
									alumno.getUrl());	*/				
					usuarios.add(user);
					
					
					
					//break;
				} else {
					new Exception("Error leyendo en file");
				}
				
				
			}
			
			users.setUser(usuarios);
			
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			System.out.println(dateFormat.format(cal.getTime()));
			String fecha = ""+dateFormat.format(cal.getTime());
			File directorio = new File("C://Chamilo//backup//"+fecha);
			directorio.mkdir();
			FileUtils.copyFileToDirectory(inputFile, directorio, Boolean.TRUE);
			
			
			
			final Serializer serializer = new Persister();

			final StringWriter writer = new StringWriter();
			serializer.write(users, writer);
			String retorno = writer.toString();
			
			System.out.println(retorno);
			
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

}
